<?php
class Banner_model extends MY_Model {
	private $module = 'banner';
	private $table = 'banner';

	function getsearchContent($limit,$page){
		$this->db->select('*');
		$this->db->limit($limit,$page);
		$this->db->order_by($this->input->post('func_order_by'),$this->input->post('order_by'));
		if($this->input->post('content')!='' && $this->input->post('content')!='type here...'){
			$this->db->where('(`title` LIKE "%'.$this->input->post('content').'%" OR `content` LIKE "%'.$this->input->post('content').'%" OR `type` = "'.$this->input->post('content').'")');
		}
		if($this->input->post('dateFrom')!='' && $this->input->post('dateTo')==''){
			$this->db->where('created >= "'.date('Y-m-d 00:00:00',strtotime($this->input->post('dateFrom'))).'"');
		}
		if($this->input->post('dateFrom')=='' && $this->input->post('dateTo')!=''){
			$this->db->where('created <= "'.date('Y-m-d 23:59:59',strtotime($this->input->post('dateTo'))).'"');
		}
		if($this->input->post('dateFrom')!='' && $this->input->post('dateTo')!=''){
			$this->db->where('created >= "'.date('Y-m-d 00:00:00',strtotime($this->input->post('dateFrom'))).'"');
			$this->db->where('created <= "'.date('Y-m-d 23:59:59',strtotime($this->input->post('dateTo'))).'"');
		}
		$query = $this->db->get(PREFIX.$this->table);

		if($query->result()){
			return $query->result();
		}else{
			return false;
		}
	}
	
	function getTotalsearchContent(){
		$this->db->select('*');
		if($this->input->post('content')!='' && $this->input->post('content')!='type here...'){
			$this->db->where('(`title` LIKE "%'.$this->input->post('content').'%" OR `content` LIKE "%'.$this->input->post('content').'%" OR `type` = "'.$this->input->post('content').'")');
		}
		if($this->input->post('dateFrom')!='' && $this->input->post('dateTo')==''){
			$this->db->where('created >= "'.date('Y-m-d 00:00:00',strtotime($this->input->post('dateFrom'))).'"');
		}
		if($this->input->post('dateFrom')=='' && $this->input->post('dateTo')!=''){
			$this->db->where('created <= "'.date('Y-m-d 23:59:59',strtotime($this->input->post('dateTo'))).'"');
		}
		if($this->input->post('dateFrom')!='' && $this->input->post('dateTo')!=''){
			$this->db->where('created >= "'.date('Y-m-d 00:00:00',strtotime($this->input->post('dateFrom'))).'"');
			$this->db->where('created <= "'.date('Y-m-d 23:59:59',strtotime($this->input->post('dateTo'))).'"');
		}
		$query = $this->db->count_all_results(PREFIX.$this->table);

		if($query > 0){
			return $query;
		}else{
			return false;
		}
	}
	
	function getDetailManagement($id){
		$this->db->select('*');
		$this->db->where('id',$id);
		$query = $this->db->get(PREFIX.$this->table);

		if($query->result()){
			return $query->result();
		}else{
			return false;
		}
	}
	
	function saveManagement($fileName=''){
		if($this->input->post('statusAdmincp')=='on'){
			$status = 1;
		}else{
			$status = 0;
		}

		if($this->input->post('hiddenIdAdmincp')==0){
			
			$data = array(
				'order_view'=> $this->input->post('orderViewAdmincp'),
				'link'=> $this->input->post('linkAdmincp'),
				'location'=> $this->input->post('locationAdmincp'),
				'image'=> $fileName,
				'status'=> $status,
				'created'=> date('Y-m-d H:i:s',time()),
				'modified'	=> date('Y-m-d H:i:s',time())
			);
			if($this->db->insert(PREFIX.$this->table,$data)){
				modules::run('admincp/saveLog',$this->module,$this->db->insert_id(),'Add new','Add new');
				return true;
			}
		}else{
			$result = $this->getDetailManagement($this->input->post('hiddenIdAdmincp'));
			
			//Xử lý xóa hình khi update thay đổi hình
			if($fileName==''){
				$fileName = $result[0]->image;
			}else{
				@unlink(DIR_BANNER.$result[0]->image);
			}
			//End xử lý xóa hình khi update thay đổi hình
			
			$data = array(
				'order_view'=> $this->input->post('orderViewAdmincp'),
				'link'=> $this->input->post('linkAdmincp'),
				'location'=> $this->input->post('locationAdmincp'),
				'image'=> $fileName,
				'status'=> $status,
				'modified'	=> date('Y-m-d H:i:s',time())
			);
			modules::run('admincp/saveLog',$this->module,$this->input->post('hiddenIdAdmincp'),'','Update',$result,$data);
			$this->db->where('id',$this->input->post('hiddenIdAdmincp'));
			if($this->db->update(PREFIX.$this->table,$data)){
				return true;
			}
		}
		return false;
	}
	
	function checkData($title){
		$this->db->select('id');
		$this->db->where('title',$title);
		$this->db->limit(1);
		$query = $this->db->get(PREFIX.$this->table);

		if($query->result()){
			return true;
		}else{
			return false;
		}
	}
	
	function checkSlug($slug){
		$this->db->select('id');
		$this->db->where('slug',$slug);
		$this->db->limit(1);
		$query = $this->db->get(PREFIX.$this->table);

		if($query->result()){
			return true;
		}else{
			return false;
		}
	}
	
	/*----------------------FRONTEND----------------------*/
	function list_top_banner()
	{
		$this->db->select('*');
		$this->db->where('status',1);
		$this->db->where('location',1);
		$this->db->order_by('order_view','DESC');
		$query = $this->db->get(PREFIX.'banner');
		if($query->num_rows() > 0)
		{
			return $query->result();
		}
		return false;
	}
	
	function list_right_banner()
	{
		$this->db->select('*');
		$this->db->where('status',1);
		$this->db->where('location',2);
		$this->db->limit(5);
		$this->db->order_by('order_view','DESC');
		$query = $this->db->get(PREFIX.'banner');
		if($query->num_rows() > 0)
		{
			return $query->result();
		}
		return false;
	}

	function list_left_banner()
	{
		$this->db->select('*');
		$this->db->where('status',1);
		$this->db->where('location',3);
		$this->db->limit(5);
		$this->db->order_by('order_view','DESC');
		$query = $this->db->get(PREFIX.'banner');
		if($query->num_rows() > 0)
		{
			return $query->result();
		}
		return false;
	}
	/*--------------------END FRONTEND--------------------*/
}